package com.aitech.hardwareconctrl

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.friendlyarm.AndroidSDK.SomeUtils
import com.google.android.exoplayer2.DefaultLoadControl
import com.google.android.exoplayer2.ExoPlaybackException
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.ExoPlayerFactory
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory

class FaceActivity : AppCompatActivity(), View.OnClickListener {
    var stopopenmouthButton: Button? = null
    var makefaceButton: Button? = null
    var makesadButton: Button? = null
    var openmouthButton: Button? = null
    var smileButton: Button? = null
    var kissButton: Button? = null
    var eyesrotateButton: Button? = null
    var blinkeyesButton: Button? = null
    var blinklefteyesButton: Button? = null
    var blinkrighteyesButton: Button? = null
    var turnNeckButton: Button? = null
    var logInfor: TextView? = null
    var TAG: String = "FaceActivity"
    private var player: ExoPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_face)
        Log.d(TAG, "  onCreate :")
        // SomeUtils.initSerialPort();
        // SomeUtils.readSerial();
        // 初始化 ExoPlayer
        player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector(), DefaultLoadControl())
        initview()
    }

    override fun onClick(view: View) {
        val id = view.id

        if (id == R.id.speak) {
            SomeUtils.speak()
        }
        if (id == R.id.stopspeak) {
            SomeUtils.stopspeak()
        }
        if (id == R.id.makeface) {
            SomeUtils.makeface()
        }
        if (id == R.id.makesadness) {
            SomeUtils.makesadness()
        }
        if (id == R.id.smile) {
            SomeUtils.smile()
        }
        if (id == R.id.kiss) {
            SomeUtils.kiss()
        }
        if (id == R.id.eyesrotate) {
            SomeUtils.eyesrotate()
        }
        if (id == R.id.blinkeyes) {
            SomeUtils.blinkeyes()
        }
        if (id == R.id.blinklefteyes) {
            SomeUtils.blinklefteyes()
        }
        if (id == R.id.blinkrighteyes) {
            SomeUtils.blinkrighteyes()
        }
        if (id == R.id.turnNeck) {
            SomeUtils.turnNeck()
        }
        if (id == R.id.speakAndAction) {
            startVoice()
        }
    }


    fun initview() {
        openmouthButton = findViewById<View>(R.id.speak) as Button
        stopopenmouthButton = findViewById<View>(R.id.stopspeak) as Button
        makefaceButton = findViewById<View>(R.id.makeface) as Button
        makesadButton = findViewById<View>(R.id.makesadness) as Button
        smileButton = findViewById<View>(R.id.smile) as Button
        kissButton = findViewById<View>(R.id.kiss) as Button
        eyesrotateButton = findViewById<View>(R.id.eyesrotate) as Button
        blinkeyesButton = findViewById<View>(R.id.blinkeyes) as Button
        blinklefteyesButton = findViewById<View>(R.id.blinklefteyes) as Button
        blinkrighteyesButton = findViewById<View>(R.id.blinkrighteyes) as Button
        turnNeckButton = findViewById<View>(R.id.turnNeck) as Button
        stopopenmouthButton!!.setOnClickListener(this)
        makefaceButton!!.setOnClickListener(this)
        makesadButton!!.setOnClickListener(this)
        openmouthButton!!.setOnClickListener(this)
        smileButton!!.setOnClickListener(this)
        kissButton!!.setOnClickListener(this)
        eyesrotateButton!!.setOnClickListener(this)
        blinkeyesButton!!.setOnClickListener(this)
        blinklefteyesButton!!.setOnClickListener(this)
        blinkrighteyesButton!!.setOnClickListener(this)
        blinkrighteyesButton!!.setOnClickListener(this)
        turnNeckButton!!.setOnClickListener(this)
        findViewById<View>(R.id.speakAndAction).setOnClickListener(this)
    }


    private val l: AudioPlayerListener = object : AudioPlayerListener {
        override fun onAudioPlayerProgress(playingPosition: Int, duration: Int, isPlaying: Boolean) {
            SomeUtils.stopspeak()
            SomeUtils.speak()
        }

        override fun onAudioPlayerPause(playingPosition: Int) {
            SomeUtils.stopspeak()
        }

        override fun onAudioPlayerCompletion() {
            SomeUtils.stopspeak()
        }
    }

    private val url = "http://music.163.com/song/media/outer/url?id=447925558.mp3"

    private fun startVoice() {
        playPrepareAsync(url)
    }

    fun playPrepareAsync(audioUrl: String?) {
        player?.playWhenReady = true

        val uri = Uri.parse(audioUrl)
        val dataSourceFactory = DefaultHttpDataSourceFactory("user-agent")
        val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
        // 播放
        player?.prepare(videoSource)
        player?.addListener(object : Player.EventListener {
            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                Log.e("ExoPlayer", "playWhenReady: $playWhenReady  +$playbackState")
                when (playbackState) {
                    Player.STATE_BUFFERING ->
                        Toast.makeText(this@FaceActivity, "加载中", Toast.LENGTH_LONG).show()

                    Player.STATE_READY -> {
                        Toast.makeText(this@FaceActivity, "播放中", Toast.LENGTH_LONG).show()
                        SomeUtils.stopspeak()
                        SomeUtils.speak()
                    }


                    Player.STATE_ENDED -> {
                        Toast.makeText(this@FaceActivity, "播放完成", Toast.LENGTH_LONG).show()
                        SomeUtils.stopspeak()
                    }
                }
            }

            override fun onPlayerError(error: ExoPlaybackException?) {
                Log.e("ExoPlayer", "ExoPlaybackException: $error")
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        player?.stop()
        player?.release()
    }

    override fun finish() {
        super.finish()
        player?.release()
    }

    override fun onStop() {
        super.onStop()
        player?.stop()
    }
}
